//
//  STEmoticonKeyboard.swift
//  STVVeibo
//
//  Created by leergou on 16/7/29.
//  Copyright © 2016年 WhiteHouse. All rights reserved.
//

import UIKit

class STEmoticonKeyboard: UIView {


    override init(frame: CGRect) {
        super.init(frame: CGRect(x: 0, y: 0, width: SCREENW, height: 216))
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: setupUI
    func setupUI(){
        backgroundColor = UIColor(patternImage: UIImage(named: "emoticon_keyboard_background")!)
        // 添加子控件
        addSubview(emoticonPageView)
        addSubview(emoticonToolbar)
        addSubview(emoticonKeyboardPageControl)
        // 添加约束
        emoticonToolbar.snp_makeConstraints { (make) -> Void in
            make.leading.trailing.bottom.equalTo(self)
            make.height.equalTo(37)
        }
        emoticonPageView.snp_makeConstraints { (make) -> Void in
            make.top.leading.trailing.equalTo(self)
            make.bottom.equalTo(emoticonToolbar.snp_top)
        }
        emoticonKeyboardPageControl.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self)
            make.bottom.equalTo(emoticonPageView.snp_bottom).offset(5)
        }
        
        // 点击底部工具条表情按钮,闭包的实现
        emoticonToolbar.closure = {[weak self] (type: STEmoticonToolbarButtonType)->() in
            
            switch type{
            case .Default:
                print("默认")
            case .Emoji:
                print("Emoji")
            case .Lxh:
                print("浪小花")
            }
            // 获得 indexPath
            let indexPath: NSIndexPath = NSIndexPath(forItem: 0, inSection: type.rawValue)
//            print(indexPath)
            // 滚动到相应的位置
            self?.emoticonPageView.scrollToItemAtIndexPath(indexPath, atScrollPosition: UICollectionViewScrollPosition.None, animated: false)
            // 点击底部表情按钮,更新 pageControl
            self?.updatePageControl(indexPath)
        }
        // 设置 pageControl
        let indexPath = NSIndexPath(forItem: 0, inSection: 0)
        updatePageControl(indexPath)
    }
    
    //MARK: lazy
    private lazy var emoticonKeyboardPageControl: UIPageControl = {
        let pageControl = UIPageControl()
        pageControl.setValue(UIImage(named: "compose_keyboard_dot_normal"), forKey: "pageImage")
        pageControl.setValue(UIImage(named: "compose_keyboard_dot_selected"), forKey: "currentPageImage")
        return pageControl
    }()
    
    private lazy var emoticonPageView: STEmoticonPageView = {
        let pageView = STEmoticonPageView()
        pageView.delegate = self
        pageView.pagingEnabled = true
        pageView.backgroundColor = self.backgroundColor
        pageView.showsHorizontalScrollIndicator = false
        pageView.showsVerticalScrollIndicator = false
        pageView.bounces = false
        return pageView
    }()
//    private lazy var emoticonToolbar: STEmoticonToolbar = STEmoticonToolbar() //  STEmoticonToolbar继承自 UIView
    private lazy var emoticonToolbar: STEmoticonToolbar = STEmoticonToolbar(frame: CGRectZero) // STEmoticonToolbar 继承自 UIStackView
}

extension STEmoticonKeyboard:UICollectionViewDelegate{
    
    func scrollViewDidScroll(scrollView: UIScrollView) {
        // 获取相对于 contentView 的屏幕中心点的 x
        let screenCenterX = scrollView.contentOffset.x + SCREENW * 0.5
        // 拿到屏幕范围内的 cell(对于此案例,最多看到两个)
        let cells = self.emoticonPageView.visibleCells().sort { (cell1, cell2) -> Bool in
            return cell1.frame.origin.x < cell2.frame.origin.x
        }
        // 如果屏幕显示2 个,就做判断
        if cells.count == 2 {
            let firstCell = cells.first!
            let secondCell = cells.last!
            // 判断哪个 cell,显示的多
            // 定义一个 indexPath
            var indexPath: NSIndexPath
            if firstCell.frame.contains(CGPoint(x: screenCenterX, y: 0)) {
                indexPath = self.emoticonPageView.indexPathForCell(firstCell)!
            } else {
                indexPath = self.emoticonPageView.indexPathForCell(secondCell)!
            }
            // 改变按钮的选中状态
            emoticonToolbar.setSelectedButtonWithTag(indexPath.section)
            
            //滚动的时候,更新 pageControl 的页码
            updatePageControl(indexPath)
        }
    }
    
    //MARK: 改变pageControl 的方法
    func updatePageControl(indexPath: NSIndexPath){
        emoticonKeyboardPageControl.numberOfPages = STEmoticonTools.emoticonSharedTools.allEmoticons[indexPath.section].count;
        emoticonKeyboardPageControl.currentPage = indexPath.item
        
    }
    
}
